package com.xiaoran.robot.controller;

import com.xiaoran.robot.entity.Msg;
import com.xiaoran.robot.service.impl.AuthServiceImpl;
import com.xiaoran.robot.utils.*;

import java.util.ArrayList;

public class FriendMessageAdaptor {
    private final String activeChar = YamlReader.instance.getValueByKey("mirai.activeChar").toString();
    private final AuthServiceImpl authService = SpringUtil.getBean(AuthServiceImpl.class);
    private final Msg msg;
    //加载词库
    public FriendMessageAdaptor(Msg msg){
        this.msg = msg;
    }

    /**
     * 消息处理
     */
    public void handleMessage(){
        System.out.println("handleMessage");
        String qid = msg.getSenderInfo().getQid();
        if (!authService.doAuth(qid, 1)){
            return;
        }
        String plainMsg = msg.getPlainMsg();
        boolean needCondition = msg.getNeedCondition();
        boolean activeDicts = msg.getActiveDicts();
        boolean isAt = msg.getIsAt();
        Integer loopNum = msg.getLoopNum();
        DictsParser dictsParser = null;
        if (activeDicts  && !needCondition) {
            // 触发了词库并且不带有条件触发前缀
            dictsParser = new DictsParser(1);
        } else if ((isAt || needCondition) && activeDicts) {
            dictsParser = new DictsParser(0);
        } else {
            // ai闲聊
            String answer = StringUtils.getAnswer(plainMsg);
            if("未获取到相关信息".equals(answer)){
                return;
            }
            ArrayList<Object> rstMsg = new RstMsgUtils().addText(answer).build();
            new MessageSender(qid, "friend").addContent(rstMsg).sendMessage();
            return;
        }
        if (dictsParser.containsKey(plainMsg)) {
            // 这里是精确查找关键词，后期应该增加模糊查找 类似： 猜拳 石头 100
            for (int i=0; i<loopNum; i++){
                ArrayList<Object> val = dictsParser.getVal(plainMsg, "friend", qid);
                new MessageSender(qid, "friend").addContent(val).sendMessage();
            }
        }
    }


}
